package com.hisan.freeride.home.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.TextWatcher;
import android.view.View;

import com.hisan.freeride.R;
import com.hisan.freeride.common.base.BaseActivity;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.databinding.SingaureBinding;

/**
 * 创建时间 : 2017/7/3
 * 创建人：yangyingqi
 * 公司：嘉善和盛网络有限公司
 * 备注：个性签名页面
 */
public class SignatureActivity extends BaseActivity<SingaureBinding> {
    TextWatcher mWatcher;
    //内容
    private String content;
    //最大字数
    private int maxWord=30;
    private Intent intent;


    @Override
    protected int getLayoutId() {
        return R.layout.singaure;
    }

    @Override
    protected void initView() {
        mBinding.ok.setEnabled(false);
        mWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (checkAllEdit()){
                    mBinding.ok.setEnabled(true);
                }else {
                    mBinding.ok.setEnabled(false);
                }
            }
            @Override
            public void afterTextChanged(Editable s) {
                int number = Integer.valueOf(s.length());
                if (number > maxWord) {
                    showToast("已到达最大字数！");
                    mBinding.etText.setText(content);
                    if (content != null && content != "") {
                        mBinding.etText.setSelection(content.length());
                    }
                    mBinding.textNumber.setText(Html.fromHtml("<font color='" + 0xFF0000 + "'>" + maxWord + "</font>" + "/" + maxWord));
                } else {
                    content = s.toString();
                    mBinding.textNumber.setText(Html.fromHtml("<font color='" + 0xb3b3b3 + "'>" + s.length() + "/" + maxWord + "</font>"));
                }
            }
        };
        mBinding.etText.addTextChangedListener(mWatcher);
    }

    @Override
    protected void initEvent() {
        mBinding.ok.setOnClickListener(v -> {
            String content1 =mBinding.etText.getText().toString();
            if (CollectionUtils.isNullOrEmpty(content1)){
                showToast("请输入内容");
                return;
            }else{
                if (CollectionUtils.isNullOrEmpty(intent))intent=new Intent();
                intent.putExtra("name", content1);
                setResult(98,intent);
                finishActivity(true);
            }
        });
        mBinding.topBack.setOnClickListener(v -> finishActivity(true));
    }

    @Override
    protected void loadData(boolean isRefresh) {}

    /**
     * 检查所有的edit是否输入了数据
     * @return
     */
    private boolean checkAllEdit() {
            if (CollectionUtils.isNullOrEmpty(mBinding.etText.getText().toString())){
            return false;}
        return true;
}
}
